from tkinter import *
import tkinter.messagebox
from tkinter import ttk
from tkinter import font
from src.backend.menu_b import MenuBackend
from src.frontend.patient_f import PatientFrontend
from src.frontend.room_f import RoomFrontend
from src.frontend.employee_f import EmployeeFrontend
from src.frontend.appointment_f import AppointmentFrontend
from src.frontend.billing_f import BillingFrontend

class MenuFrontend:
    """Frontend UI for menu management"""
    
    def __init__(self, master, user_type='admin'):
        self.master = master
        self.user_type = user_type
        self.backend = MenuBackend()
        self.current_window = None  # Track current open window
        
        self.master.title("HOSPITAL MANAGEMENT SYSTEM")
        self.master.geometry("800x600+0+0")
        self.master.config(bg="#C0C0C0")
        self.frame = Frame(self.master, bg="#C0C0C0")
        self.frame.pack()

        self.create_ui()

    def create_ui(self):
        """Create the user interface"""
        # Title
        self.lblTitle = Label(self.frame, text="HOSPITAL MANAGEMENT SYSTEM", font="Arial 16 bold", bg="#C0C0C0", fg="#000080")
        self.lblTitle.grid(row=0, column=0, columnspan=2, pady=40)
        
        # Menu frame
        self.MenuFrame = Frame(self.frame, width=400, height=80, relief="groove", bg="#C0C0C0", bd=3)
        self.MenuFrame.grid(row=1, column=0, padx=20, pady=20)
        
        # Create menu buttons
        self.create_menu_buttons()

    def create_menu_buttons(self):
        """Create menu buttons based on user type"""
        # Get available modules
        modules = self.backend.get_available_modules(self.user_type)
        
        # Create buttons for each module
        row = 0
        col = 0
        
        for i, module in enumerate(modules):
            if module == 'Patient Registration':
                btn = Button(self.MenuFrame, text=module, width=22, font="Arial 11 bold", 
                           bg="#C0C0C0", relief="raised", bd=2, command=self.open_patient_form)
            elif module == 'Room Allocation':
                btn = Button(self.MenuFrame, text=module, width=22, font="Arial 11 bold", 
                           bg="#C0C0C0", relief="raised", bd=2, command=self.open_room_form)
            elif module == 'Employee Registration':
                btn = Button(self.MenuFrame, text=module, width=22, font="Arial 11 bold", 
                           bg="#C0C0C0", relief="raised", bd=2, command=self.open_employee_form)
            elif module == 'Appointment Booking':
                btn = Button(self.MenuFrame, text=module, width=22, font="Arial 11 bold", 
                           bg="#C0C0C0", relief="raised", bd=2, command=self.open_appointment_form)
            elif module == 'Billing':
                btn = Button(self.MenuFrame, text=module, width=22, font="Arial 11 bold", 
                           bg="#C0C0C0", relief="raised", bd=2, command=self.open_billing_form)
            else:
                btn = Button(self.MenuFrame, text=module, width=22, font="Arial 11 bold", 
                           bg="#C0C0C0", relief="raised", bd=2, command=lambda: self.show_coming_soon(module))
            
            btn.grid(row=row, column=col, padx=10, pady=10)
            
            col += 1
            if col > 1:  # Two columns
                col = 0
                row += 1
        
        # Exit button
        self.button_exit = Button(self.MenuFrame, text="EXIT", width=22, font="Arial 11 bold", 
                                bg="#C0C0C0", relief="raised", bd=2, command=self.Exit)
        self.button_exit.grid(row=row+1, column=0, columnspan=2, pady=20)

    def open_patient_form(self):
        """Open patient registration form"""
        try:
            if self.current_window:
                self.current_window.destroy()
            
            self.current_window = Toplevel(self.master)
            self.patient_app = PatientFrontend(self.current_window, self.user_type)
        except Exception as e:
            tkinter.messagebox.showerror("HOSPITAL DATABASE SYSTEM", f"Error opening patient form: {str(e)}")

    def open_room_form(self):
        """Open room allocation form"""
        try:
            if self.current_window:
                self.current_window.destroy()
            
            self.current_window = Toplevel(self.master)
            self.room_app = RoomFrontend(self.current_window, self.user_type)
        except Exception as e:
            tkinter.messagebox.showerror("HOSPITAL DATABASE SYSTEM", f"Error opening room form: {str(e)}")

    def open_employee_form(self):
        """Open employee registration form"""
        try:
            if self.current_window:
                self.current_window.destroy()
            
            self.current_window = Toplevel(self.master)
            self.employee_app = EmployeeFrontend(self.current_window, self.user_type)
        except Exception as e:
            tkinter.messagebox.showerror("HOSPITAL DATABASE SYSTEM", f"Error opening employee form: {str(e)}")

    def open_appointment_form(self):
        """Open appointment booking form"""
        try:
            if self.current_window:
                self.current_window.destroy()
            
            self.current_window = Toplevel(self.master)
            self.appointment_app = AppointmentFrontend(self.current_window, self.user_type)
        except Exception as e:
            tkinter.messagebox.showerror("HOSPITAL DATABASE SYSTEM", f"Error opening appointment form: {str(e)}")

    def open_billing_form(self):
        """Open billing form"""
        try:
            if self.current_window:
                self.current_window.destroy()
            
            self.current_window = Toplevel(self.master)
            self.billing_app = BillingFrontend(self.current_window, self.user_type)
        except Exception as e:
            tkinter.messagebox.showerror("HOSPITAL DATABASE SYSTEM", f"Error opening billing form: {str(e)}")

    def show_coming_soon(self, module):
        """Show coming soon message for unavailable modules"""
        tkinter.messagebox.showinfo("HOSPITAL DATABASE SYSTEM", f"{module} module is coming soon!")

    def Exit(self):
        """Exit the application"""
        if self.current_window:
            self.current_window.destroy()
        self.master.destroy()

